<?php
include('db.class.php');
class User {
	const SUCCESS = 800;
	const FAIL = 0;
	const DATA_EXISTS = 1;
	
	function login($username, $password) {
		$db = new db();
		$results = $db->select("SELECT user_id, username, user_type FROM users WHERE username = '$username' AND user_password = '$password'");
		if($results != 0) {
			//over here, create a session
			$_SESSION['user_id'] = $results[0]['user_id'];
			$_SESSION['username'] = $results[0]['username'];
			if($results[0]['user_type'] == 1) {
				$_SESSION['user_type'] = 1;
			}
			return self::SUCCESS;
		}
		return self::FAIL;
		//$results will be an array with the results inside
	}
	
	function register($username, $password, $fname, $lname, $email) {
		$db = new db();
		$result = $db->insert("INSERT INTO users(username, user_password) VALUES('$username', '$password')");
		if($result) {
			$profile = $db->insert("INSERT INTO user_profiles(profile_fname, profile_lname, profile_email) VALUES('$fname', '$lname', '$email')");
			if($profile != self::FAIL) { 
				return self::SUCCESS;
			}
			return self::DATA_EXISTS;
		}
		//else return something that says no
		return self::FAIL;
	}
	
	function deleteUser($user_id) {
		$db = new db();
		$results = $db->delete("DELETE FROM users WHERE user_id = '$user_id'");
		if($results == self::SUCCESS) {
			return self::SUCCESS;
		}
		return self::FAIL;
	}
	
	function selectAll() {
		$db = new db();
		$user_id = $_SESSION['user_id'];
		$results = $db->select("SELECT user_id, username FROM users WHERE user_id != '$user_id'");
		if($results != 0) {
			return $results;
		}
		return self::FAIL;
	}
	
	function checkExist($user) {
		$db = new db();
		$results = $db->select("SELECT username FROM users WHERE username = '$user'");
		if($results != 0) {
			return self::DATA_EXISTS;
		}
		return self::FAIL;
	}
	
	function searchUser($search) {
		$db = new db();
		$results = $db->select("SELECT user_id, username FROM users"); // WHERE username LIKE '1%'
		if($results != 0) {
			return $results;
		}
		return self::FAIL;
	}
}
?>